﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QND
{
    using Microsoft.EnterpriseManagement.HealthService;
    using System.Xml;
    using Microsoft.EnterpriseManagement.Mom.Internal.Xml;
    using System.Diagnostics;

    [MonitoringDataType]
    public class EncodedDataItem : DataItemBase
    {
        /// <summary>
        /// Data to be persisted and also posted on output stream
        /// </summary>
        private string encodedItem;

        /// <summary>
        /// The name of the XML element we store our data in.
        /// </summary>
        private const string encodedItemTag = "EncodedItem";

        /// <summary>
        /// Constructor to create a new instance of this data item.
        /// </summary>
        /// <param name="item">The value used to fill the output dataitem html encoded</param>
        public EncodedDataItem(string item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            this.encodedItem = item;
        }

        /// <summary>
        /// Constructor for creating this data item from XML.  When the base
        /// class constructor returns the reader will be positioned at the
        /// start of the data for this object.
        /// </summary>
        /// <param name="reader">XmlReader containing the XML version of this data item.</param>
        public EncodedDataItem(XmlReader reader)
            : base(reader)
        {
            // Read our portion of the XML document.  If the XML is invalid we
            // must throw MalformedDataItemException.  It doesn't apply for
            // this data item since we don't do additional validation but all
            // data must be validated and any failures must result in
            // MalformedDataItemException being thrown.  Known transient
            // failures must result in ModuleException being thrown.  Other
            // exception types may cause the host process to be taken down.
            try
            {
                this.encodedItem = reader.ReadElementString(encodedItemTag);
                /* This is from an internal OPsMgr module but XmlreaderHelper is defined in Microsoft.Mom.Common and Microdoft.EnterpriseManagement.HealthService.Utility
                 *                 XmlReaderHelper xmlReaderHelper = new XmlReaderHelper(reader);
                this.operation = (ulong)xmlReaderHelper.ReadElementAsUInt32("Operation");
                this.description = reader.ReadElementString("Description");
                this.agentName = reader.ReadElementString("AgentName");
                this.principalName = reader.ReadElementString("PrincipalName");
                this.qferesult = (ulong)xmlReaderHelper.ReadElementAsUInt32("SoftwareUpdateResult");
                this.softwareUpdateInstalled = reader.ReadElementString("SoftwareUpdateInstalled");
                this.softwareUpdateNotInstalled = reader.ReadElementString("SoftwareUpdateNotInstalled");
                this.softwareUpdateFailureDesc = reader.ReadElementString("SoftwareUpdateFailureDesc");
                 * */
            }
            catch (XmlException xe)
            {
                throw new MalformedDataItemException(
                    "This should be localized text about invalid XML in production.", xe);
            }

            Debug.Assert(this.encodedItem != null);
        }

        /// <summary>
        /// This is the name of the data item as specified in the MP.
        /// </summary>
        public override string DataItemTypeName
        {
            get
            {
                return "QND.EncodedDataItem";
            }
        }

        /// <summary>
        /// Data to be persisted and also posted on output stream.  Since data
        /// items are invariant there is not set a accessor.
        /// </summary>
        public string EncodedItem
        {
            get
            {
                return this.encodedItem;
            }
        }

        /// <summary>
        /// This writes out the portion of the XML that is specific to this
        /// derived data item.
        /// </summary>
        /// <param name="writer">XML stream to write to.</param>
        protected override void GenerateItemXml(XmlWriter writer)
        {
            // We just need to write out our portion of the XML document.  The
            // base class has handled writing out its portion before it called
            // into this method.
            writer.WriteElementString(
                EncodedDataItem.encodedItemTag,
                this.encodedItem);
            /// 
            /* From OpsMgr INternal class
             *  XmlWriterHelper xmlWriterHelper = new XmlWriterHelper(writer);
                xmlWriterHelper.WriteElement("Description", this.description);
                xmlWriterHelper.WriteElement("AgentName", this.agentName);
                xmlWriterHelper.WriteElement("PrincipalName", this.principalName);
                xmlWriterHelper.WriteElement("ErrorCode", this.errorCode);
                xmlWriterHelper.WriteElement("Operation", (double)((float)this.operation));
                xmlWriterHelper.WriteElement("SoftwareUpdateResult", (double)((float)this.qferesult));
                xmlWriterHelper.WriteElement("SoftwareUpdateInstalled", this.softwareUpdateInstalled);
                xmlWriterHelper.WriteElement("SoftwareUpdateNotInstalled", this.softwareUpdateNotInstalled);
                xmlWriterHelper.WriteElement("SoftwareUpdateFailureDesc", this.softwareUpdateFailureDesc);
             * */
            ///
        }
    }
}
